import { Timer } from './../../node_modules/app-builder-lib/out/util/timer.d';
import { app, BrowserWindow, ipcMain ,screen, protocol, net } from 'electron';
import * as path from 'path';
let mainWindow: BrowserWindow | null;
import * as fs from 'fs';
import axios from 'axios';
console.log('NODE_ENV', process.env.NODE_ENV);
const url = require('url');
const indexFile = path.join('file:', __dirname, '/../renderer/index.html');
const fileUrl = new URL(indexFile).href;

/**
 *
 */

function createWindow(): BrowserWindow {
  // Create the browser window.
  const preLoadPath = path.join(__dirname, 'preload.js')
  console.log("preload path", preLoadPath)

  let userWindow: null | BrowserWindow = new BrowserWindow({
    height: 650,
    width: 1200,

    // frame:false,
    // transparent: true,
    // hasShadow:false,

    webPreferences: {
      contextIsolation: true,
      nodeIntegration: false,
      webSecurity: false,
      allowRunningInsecureContent: true,

      preload: preLoadPath,
    },

  });

  // and load the index.html of the app.
  // mainWindow.loadFile(path.join(__dirname, '../html/index.html'));
  if (process.env.NODE_ENV === 'development') {
    userWindow.loadURL('http://127.0.0.1:3000');
    // userWindow.loadURL('https://mms.pinduoduo.com/chat-merchant/index.html?r=0.6717724803853746#/');
    // userWindow.loadURL('https://opt.in2magic.cn/');
  } else {
    console.log('indexFile', fileUrl);
    userWindow.loadURL(fileUrl);
  }
  // Open the DevTools.
//   userWindow.webContents.openDevTools();
  userWindow.webContents.on('dom-ready', () => {

  });
  // Emitted when the window is closed.
  userWindow.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    userWindow = null;
  });
  const {width,height} = screen.getPrimaryDisplay().workAreaSize;//获取到屏幕的宽度和高度
  //width,height 1792 1035
  console.log("width,heightwidth,heightwidth,height",width,height)
  let bounds = userWindow.getBounds();

  console.log("parseInt(String(width / 20)",parseInt(String(width / 20)))
  userWindow.setPosition(parseInt(String(width / 2))-( bounds.width/2),200,true)

  return userWindow;
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

 
app.whenReady().then(() => {
  // windowMove(mainWindow)

});
const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
  console.log('get lock fial, do quit ');
  app.quit();
} else {
  app.on('second-instance', () => {
    // Print out data received from the second instance.
    // Someone tried to run a second instance, we should focus our window.
    if (mainWindow) {
      if (mainWindow.isMinimized()) {
        mainWindow.restore();
      }
      mainWindow.focus();
    }
  });

  // Create myWindow, load the rest of the app, etc...
  app.whenReady().then(() => {
    mainWindow = createWindow();
    ipcMain.on('set-ignore-mouse-events', (event, ignore, options) => {
      const win = BrowserWindow.fromWebContents(event.sender)
      if(win){
        win.setIgnoreMouseEvents(ignore, options)
      }
    })
  });
}
app.on('activate', () => {
  // On OS X it"s common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    mainWindow = createWindow();
  }
  if (BrowserWindow.getAllWindows().length === 0) {
    mainWindow = createWindow();
  }
});
ipcMain.on('asynchronous-message', function (event, arg) {
  console.log(arg); // prints "ping"
});
